package cn.gdcp.recyclerview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText edtName;
    private EditText edtAge;
    private RadioButton radiobutton1;
    private RadioButton radiobutton2;
    private RadioButton radiobutton3;
    private RadioButton radiobutton4;
    private RadioButton radiobutton5;
    private Button btnSave;
    private String no;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        setTitle("Edit Student");
        findViews();
        initViewData();
        BtnSaveListener();
    }

    private void findViews() {
        edtName = findViewById(R.id.edtName);
        edtAge = findViewById(R.id.edtAge);

        radiobutton1 = findViewById(R.id.radioBtn1);
        radiobutton2 = findViewById(R.id.radioBtn2);
        radiobutton3 = findViewById(R.id.radioBtn3);
        radiobutton4 = findViewById(R.id.radioBtn4);
        radiobutton5 = findViewById(R.id.radioBtn5);
        btnSave = findViewById(R.id.btn_save);
    }
    private void initViewData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE", 0);
        int imgId = intent.getIntExtra("IMGID", R.drawable.dog1);
        no = intent.getStringExtra("NO");

        edtName.setText(name);
        edtAge.setText(String.valueOf(age));
        switch (imgId) {
            case R.drawable.dog1:
                radiobutton1.setChecked(true);
                break;
            case R.drawable.dog2:
                radiobutton1.setChecked(true);
                break;
            case R.drawable.dog3:
                radiobutton1.setChecked(true);
                break;
            case R.drawable.dog4:
                radiobutton1.setChecked(true);
                break;
            case R.drawable.dog5:
                radiobutton1.setChecked(true);
                break;
        }
    }
    private void BtnSaveListener() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtName.getText().toString();
                int age = Integer.parseInt(edtAge.getText().toString());

                int imgId = R.drawable.dog1;
                if (radiobutton1.isChecked()) {
                    imgId = R.drawable.dog1;
                } else if (radiobutton2.isChecked()) {
                    imgId = R.drawable.dog2;
                } else if (radiobutton3.isChecked()) {
                    imgId = R.drawable.dog3;
                } else if (radiobutton4.isChecked()) {
                    imgId = R.drawable.dog4;
                } else if (radiobutton5.isChecked()) {
                    imgId = R.drawable.dog5;
                }

                Intent intent = new Intent();
                intent.putExtra("NAME", name);
                intent.putExtra("AGE", age);
                intent.putExtra("IMG", imgId);
                intent.putExtra("NO", no);
                setResult(3001, intent);
                finish();
            }
        });
    }
}
